package com.learn.project2.service.wx.address;

import com.learn.project2.bean.*;
import com.learn.project2.mapper.MarketAddressMapper;
import com.learn.project2.mapper.MarketRegionMapper;
import com.learn.project2.model.bo.wx.adress.AddressSaveBO2;
import com.learn.project2.model.vo.wx.address.AddressListVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

import static org.apache.shiro.SecurityUtils.getSubject;

/**
 * @description：
 * @author： Yj
 * @create： 2022/01/09
 **/
@Service
public class AddressServiceImpl2 implements AddressService2{

    @Autowired
    MarketAddressMapper addressMapper;

    @Autowired
    MarketRegionMapper regionMapper;

    /**
     * @description: 小程序收货地址list
     * @param:
     * @return:
     * @author wxy
     * @date: 2022-01-10 14-48
     */
    @Override
    public AddressListVO2 selectList() {
        MarketUser user = (MarketUser) getSubject().getPrincipal();
        MarketAddressExample addressExample = new MarketAddressExample();
        //获得list的total
        long total = addressMapper.countByExample(addressExample);
        AddressListVO2 addressListVO2 = new AddressListVO2();
        addressListVO2.setLimit((int) total);
        addressListVO2.setPage(1);
        addressListVO2.setPages(1);
        addressListVO2.setTotal((int) total);
        //获取list的信息
        addressExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(user.getId());
        List<MarketAddress> list = addressMapper.selectByExample(addressExample);

        addressListVO2.setList(list);

        return addressListVO2;
    }
    /**
 * @description: 获取收货地址详情
 * @param:  
 * @return:  
 * @author 亓永杰
 * @date: 2022-01-11 09:49
 */ 
    @Override
    public MarketAddress detail(@RequestBody Integer id) {
        MarketAddress marketAddress = addressMapper.selectByPrimaryKey(id);
        return marketAddress;
    }
/** 
 * @description: 保存收货地址（添加和编辑）
 * @param:  
 * @return:  
 * @author 亓永杰
 * @date: 2022-01-11 09:51
 */ 
    @Override
    public int save(AddressSaveBO2 addressSaveBO2) {

        MarketUser user = (MarketUser) getSubject().getPrincipal();
        MarketAddress address = new MarketAddress();

        address.setAddressDetail(addressSaveBO2.getAddressDetail());
        address.setAreaCode(addressSaveBO2.getAreaCode());
        address.setCity(addressSaveBO2.getCity());
        address.setCounty(addressSaveBO2.getCounty());
        address.setId(addressSaveBO2.getId());
        address.setIsDefault(addressSaveBO2.getIsDefault());
        address.setName(addressSaveBO2.getName());
        address.setProvince(addressSaveBO2.getProvince());
        address.setTel(addressSaveBO2.getTel());
        address.setUpdateTime(new Date());
        address.setUserId(user.getId());

        MarketRegionExample regionExample = new MarketRegionExample();
        regionExample.createCriteria().andNameEqualTo(addressSaveBO2.getCity());
        List<MarketRegion> regions = regionMapper.selectByExample(regionExample);
        MarketRegion region = regions.get(0);

        address.setPostalCode(region.getCode().toString());
        address.setAddTime(new Date());
        if (addressSaveBO2.getId() != 0) {
            addressMapper.updateByPrimaryKeySelective(address);
        }else {
            address.setId(null);
            addressMapper.insertSelective(address);
        }
        //判断默认地址
         if (addressSaveBO2.getIsDefault()){
             MarketAddress address1 = new MarketAddress();
             address1.setIsDefault(false);
             MarketAddressExample addressExample = new MarketAddressExample();
             addressExample.createCriteria().andIdNotEqualTo(address.getId()).andIsDefaultEqualTo(true).andUserIdEqualTo(user.getId());
             addressMapper.updateByExampleSelective(address1,addressExample);
         }

        return address.getId();
    }
/**
 * @description: 删除收货地址（逻辑删除）
 * @param:
 * @return:
 * @author 亓永杰
 * @date: 2022年1月11日16:02:26
 */
    @Override
    public void delete(Integer id) {
        MarketAddress address = new MarketAddress();
        address.setId(id);
        address.setDeleted(true);
        addressMapper.updateByPrimaryKeySelective(address);
    }


}
